package com.pfzy.ui.activity.fragmentationx;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.pfzy.ui.R;
import com.pfzy.ui.databinding.ActivityBannerBinding;
import com.pfzy.ui.databinding.ActivityFragmentationxBinding;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

import me.yokeyword.fragmentation.SupportActivity;

public class FragmentationxActivity extends SupportActivity implements View.OnClickListener {
    private ActivityFragmentationxBinding mBind;
    private String[] mTitle= {"页面1", "页面2"};
    private ArrayList<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mBind = ActivityFragmentationxBinding.inflate(getLayoutInflater());
        setContentView(mBind.getRoot());
        initView();
    }

    private void initView() {
        mFragments.clear();
        mFragments.add(new ViewPagerFragment1());
        mFragments.add(new ViewPagerFragment2());

        TextView title = mBind.toolbar.findViewById(R.id.layout_toolbar_title);
        title.setText("fragment");
        mBind.toolbar.findViewById(R.id.layout_toolbar_back).setOnClickListener(this);

        mBind.actFragmentationxViewpager.setAdapter(new FragmentStateAdapter(getSupportFragmentManager(), getLifecycle()) {
            @Override
            public int getItemCount() {
                return mFragments.size();
            }

            @NonNull
            @NotNull
            @Override
            public Fragment createFragment(int position) {
                return mFragments.get(position);
            }
        });
        mBind.actFragmentationxViewpager.setOffscreenPageLimit(1);

        TabLayoutMediator mediator = new TabLayoutMediator(mBind.actFragmentationxTablayout, mBind.actFragmentationxViewpager, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(mTitle[position]);
            }
        });
        mediator.attach();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.layout_toolbar_back:
                finish();
                break;
            default:
                break;
        }
    }
}
